//
//  Followee.m
//  TwitterFon
//
//  Created by kaz on 11/2/08.
//  Copyright 2008 naan studio. All rights reserved.
//

#import "Followee.h"
#import "GuaziAppDelegate.h"
#import "DBConnection.h"
#import "pinyin.h"

@interface NSObject (FolloweeDelegate)
- (void)followeeDidGetFollowee:(Followee*)Followee;
@end

@implementation Followee

@synthesize userId;
@synthesize name;
@synthesize screenName;
@synthesize profileImageUrl;
@synthesize pyLetters;


- (void)dealloc
{
    [name release];
    [screenName release];
    [profileImageUrl release];
    [pyLetters release];
    [super dealloc];
}

+ (Followee*)initWithStatement:(Statement*)stmt
{
    Followee *followee       = [[Followee alloc] init];
    followee.userId          = [stmt getInt32:0];
    followee.name            = [stmt getString:1];
    followee.screenName      = [stmt getString:2];
    followee.profileImageUrl = [stmt getString:3];
    
    return followee;
}


- (NSString*) pyLetters
{
    if ( pyLetters == nil ) {
        NSString* s = self.screenName;
        if ( s==nil || [s length]==0 ) {
            pyLetters = [@"" retain];
        }
        else {
            NSMutableString* py = [NSMutableString stringWithString:@""];
            for ( int i=0; i<[s length]; i++ ) {
                unichar c = [s characterAtIndex:i];
                if ( (c >= '0'&& c<='9') || (c>='a'&&c<='z') || (c>='A'&&c<='Z') ) {
                    [py appendString:[[NSString stringWithFormat:@"%c", c] lowercaseString]];
                }
                else {
                    [py appendString:[NSString stringWithFormat:@"%c", pinyinFirstLetter(c)]];
                }
            }
            pyLetters = [py retain];
        }
    }
    return pyLetters;
}


- (NSString*) firstLetter
{
    NSString* py = self.pyLetters;
    if ( py == nil || [py length] == 0 ) return @"#";
    
    NSRange r;
    r.location = 0;
    r.length = 1;
    return [py substringWithRange:r];
}


+ (void)updateDB:(User*)user
{
    if (user.following) {
        [Followee insertDB:user];
    }
    else {
        [Followee deleteFromDB:user];
    }
}

+ (void)insertDB:(User*)user
{
	sqlite3* db = [DBConnection getDatabase];
	char* sql = "REPLACE INTO followees VALUES(?, ?, ?, ?)";
	
    Statement *stmt = [[Statement alloc] initWithDB:db sql:sql];
    [stmt bindInt64:user.userId             forIndex:1];
    [stmt bindString:user.name              forIndex:2];
    [stmt bindString:user.screenName        forIndex:3];
    [stmt bindString:user.profileImageUrl_s   forIndex:4];

    if ([stmt step] != SQLITE_DONE) {
        [GuaziAppDelegate showAlert:@"数据库访问错误！Followee.insertDB"];
    }
    [stmt release];
}


+ (void)deleteFromDB:(User*)user
{
 	sqlite3* db = [DBConnection getDatabase];
	char* sql = "DELETE FROM followees WHERE user_id = ?";
	
    Statement *stmt = [[Statement alloc] initWithDB:db sql:sql];
    [stmt bindInt32:user.userId forIndex:1];

    if ([stmt step] != SQLITE_DONE) {
        [GuaziAppDelegate showAlert:@"数据库访问错误！Followee.deleteFromDB"];
    }
    
    [stmt release];
}

@end
